package com.jinlejia.feedback.idea.ui;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.Gravity;
import android.view.Window;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.feedback.R;
import com.jinlejia.feedback.common.FbApiConfig;
import com.jinlejia.feedback.idea.ui.adapter.FBFilter1Adapter;
import com.jinlejia.feedback.idea.ui.adapter.FBFilter2Adapter;
import com.jinlejia.feedback.idea.ui.bean.FbQuestionTwoBean;
import com.weight.CustomToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * @author SongJiaTai
 * @description:二级联动弹窗
 * @date :2019/6/25 17:44
 */
public class QuestionFengleiDialog extends Dialog{
    private Context mContext;

    //第一列
    private ArrayList<Map<String, Object>> data1;
    private FBFilter1Adapter fbFilter1Adapter1;


    //第二列
    private ArrayList<Map<String, Object>> data2 = new ArrayList<>();
    private FBFilter2Adapter filter1Adapter2;

    private String questionOne;

    public QuestionFengleiDialog(@NonNull Context context,ArrayList<Map<String, Object>> data1) {
        super(context);
        this.mContext = context;
        this.data1 = data1;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        window.setContentView(R.layout.fb_dialog_questionfenglei);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        YfListRecyclerView oneRecycle = window.findViewById(R.id.one_recycler_view);
        YfListRecyclerView twoRecycle = window.findViewById(R.id.two_recycle_view);
        oneRecycle.setLayoutManager(new LinearLayoutManager(mContext));
        twoRecycle.setLayoutManager(new LinearLayoutManager(mContext));
        fbFilter1Adapter1 =  new FBFilter1Adapter(mContext,data1);
        filter1Adapter2 = new FBFilter2Adapter(mContext,new ArrayList<Map<String, Object>>());
        oneRecycle.setAdapter(fbFilter1Adapter1);
        twoRecycle.setAdapter(filter1Adapter2);

        fbFilter1Adapter1.setData(data1);
        fbFilter1Adapter1.setOnItemSelectedListener(new FBFilter1Adapter.OnItemSelectedListener() {
            @Override
            public void onItemSelected(Map<String, Object> map) {
                String  problemTypeCode = (String) map.get("problemTypeCode");
                questionOne = (String) map.get("problemTypeName");
                getData(problemTypeCode);

            }
        });

        fbFilter1Adapter1.setOnItemRemoveListener(new FBFilter1Adapter.OnItemRemoveListener() {
            @Override
            public void onItemRemove(Map<String, Object> map) {
            }
        });

        filter1Adapter2.setOnItemSelectedListener(new FBFilter2Adapter.OnItemSelectedListener() {
            @Override
            public void onItemSelected(String s) {
                onCheckClickListener.onItemclick(questionOne,s);
            }
        });




    }


    public  interface OnCheckClickListener{
        void onItemclick(String questionOnew,String questionTwo);
    }

    private  OnCheckClickListener onCheckClickListener;

    public void setOnCheckClickListener(OnCheckClickListener listener){
        this.onCheckClickListener = listener;
    }



    private void getData(String problemTypeCode){
        OkHttpUtils.get()
                   .url(FbApiConfig.SecondLevelClassificationOfProblems)
                   .addParams("firstLevelCode",problemTypeCode)
                   .build()
                   .execute(new MapCallBack() {
                       @Override
                       public void onError(Call call, Exception e, int id) {
                           CustomToast.showToast(mContext,"网络连接失败",1500);
                       }

                       @Override
                       public void onResponse(Map<String, Object> response, int id) {
                           JSONArray dataList = (JSONArray) response.get("data");
                           List<FbQuestionTwoBean> fbQuestionTwoBeansList = JSON.parseArray(dataList.toString(),FbQuestionTwoBean.class);
                           data2.clear();

                           for (FbQuestionTwoBean fbQuestionTwoBean : fbQuestionTwoBeansList) {
                               Map<String, Object> map = new HashMap<>();
                               map.put("problemTypeName", fbQuestionTwoBean.getProblemTypeName());
                               map.put("problemTypeCode", fbQuestionTwoBean.getProblemTypeCode());
                               data2.add(map);
                           }

                           if (data2.size() > 0) {
                               filter1Adapter2.cur_position = -1;
                               filter1Adapter2.setData(data2);
                           }
                       }
                   });
    }
}
